//
//  UIButton+Helper.m
//  ketuitui
//
//  Created by apple on 14-10-25.
//  Copyright (c) 2014年 LAURA. All rights reserved.
//

#import "UIButton+Helper.h"
#import "UIImage+Creation.h"
@implementation UIButton (Helper)

+(UIButton *) buttonWithFrame: (CGRect) frame
                        title: (NSString *) title
                   titleColor: (UIColor *) titleColor
                    titleFont: (UIFont *) font
                        image: (UIImage *) image
              backGroundColor: (UIColor *) backGroundColor
              backGroundImage: (UIImage *) backGroundImage{
    UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
    button.frame = frame;
    [button setImage: image forState: UIControlStateNormal];
    [button setBackgroundImage: backGroundImage forState: UIControlStateNormal];
    [button setBackgroundColor: backGroundColor];
    [button setTitle: title forState: UIControlStateNormal];
    [button setTitleColor: titleColor forState: UIControlStateNormal];
    button.titleLabel.font = font;
    return button;
}

//做一个圆角的button 根据title 的长短来设置button 的长短  根据字号大小来设置btton 的高度

+(UIButton *) buttonWithFrame: (CGRect) frame
                        title: (NSString *) title
                   titleColor: (UIColor *) titleColor
                    titleFont: (UIFont *) font
              backGroundColor: (UIColor *) backGoundColor
               backGoundImage: (UIImage *) backGroundImage
                selectedColor: (UIColor *) selectedColor
                  borderColor: (UIColor *) borderColor
                  borderwidth: (CGFloat) borderWidth{
    return [self buttonWithFrame: frame
                           title: title
                      titleColor: titleColor
                       titleFont: font
                           image: nil
                 backGroundColor: backGoundColor
                  backGoundImage: backGroundImage
                   selectedColor: selectedColor
                     borderColor: borderColor
                     borderwidth: borderWidth];
}

+(UIButton *) buttonWithFrame: (CGRect) frame
                        title: (NSString *) title
                   titleColor: (UIColor *) titleColor
                    titleFont: (UIFont *) font
                        image: (UIImage *) image
              backGroundColor: (UIColor *) backGoundColor
               backGoundImage: (UIImage *) backGroundImage
                selectedColor: (UIColor *) selectedColor
                  borderColor: (UIColor *) borderColor
                  borderwidth: (CGFloat) borderWidth{
    UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
    button.frame = frame;
    if (title){
        [button setTitle: title forState: UIControlStateNormal];
        button.titleLabel.font = font;
        NSDictionary *attributes = @{NSFontAttributeName : font};
        CGFloat with = [[[NSAttributedString alloc] initWithString: title attributes: attributes] size].width;
        CGRect rect = CGRectMake( button.frame.origin.x , button.frame.origin.y , with + 20 , font.pointSize + 10 );
        button.frame = rect;
    }
    if (image){
        [button setImage: image forState: UIControlStateNormal];
    }
    if (backGroundImage){
        [button setBackgroundImage: backGroundImage forState: UIControlStateNormal];
    }
    if (backGoundColor){
        [button setBackgroundColor: backGoundColor];
    }
    if (titleColor){
        [button setTitleColor: titleColor forState: UIControlStateNormal];
    }
    if (borderColor){
        button.layer.borderColor = borderColor.CGColor;
    }
    if (borderWidth){
        button.layer.borderWidth = borderWidth;
    }
    if (selectedColor){
        [button setTitleColor: selectedColor forState: UIControlStateSelected];
    }

    button.layer.cornerRadius = 10;
    return button;
}

+(UIButton *) buttonWithFrame: (CGRect) frame
              centerImageName: (NSString *) imageName
              backGroundColor: (UIColor *) backGroundColor
                  borderColor: (UIColor *) borderColor
                  borderWidth: (CGFloat) borderWidth{
    return [self buttonWithFrame: frame
                           title: nil
                      titleColor: nil
                       titleFont: nil
                           image: [UIImage imageNamed: imageName]
                 backGroundColor: backGroundColor
                  backGoundImage: nil
                   selectedColor: nil
                     borderColor: borderColor
                     borderwidth: borderWidth];
}

+(UIButton *) buttonWithFrame: (CGRect) frame
                    imageName: (NSString *) imageName
                        title: (NSString *) title
                    TitleFont: (int) font
                    textColor: (UIColor *) color{
    UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
    button.frame = frame;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake( 0 , 0 , frame.size.width , frame.size.height * 0.6 )];
    imageView.image = [UIImage imageNamed: imageName];

    UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake( - 15 , imageView.frame.size.height + imageView.frame.origin.y , frame.size.width + 30 , frame.size.height * 0.4 )];
    label.text = title;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize: font];
    label.textColor = color;
    [button addSubview: imageView];
    [button addSubview: label];
    return button;
}

+(UIButton *) buttonWithFrame: (CGRect) frame color: (UIColor *) normalColor highlightColor: (UIColor *) highlightColor cornerRadius: (CGFloat) cornerRadius titleFont: (int) font titleColor: (UIColor *) titleColor Title: (NSString *) title{
    UIButton *button = [UIButton buttonWithFrame: frame title: title titleColor: titleColor titleFont: [UIFont systemFontOfSize: font] image: nil backGroundColor: nil backGroundImage: nil];
    [button setImage: [UIImage imageWithColor: normalColor size: button.frame.size] forState: UIControlStateNormal];
    [button setImage: [UIImage imageWithColor: highlightColor size: button.frame.size] forState: UIControlStateHighlighted];

    button.layer.cornerRadius = cornerRadius;
    button.layer.masksToBounds = YES;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame: CGRectMake( 0 , 0 , frame.size.width , frame.size.height )];
    titleLabel.textAlignment = 1;
    titleLabel.text = title;
    titleLabel.textColor = titleColor;
    titleLabel.font = [UIFont systemFontOfSize: font];
    [button addSubview: titleLabel];

    return button;
}

@end
